package com.maserb.monitor.web.schoolConduct;

import com.maserb.monitor.common.*;
import com.maserb.monitor.entity.JwOrgEntity;
import com.maserb.monitor.entity.UserEntity;
import com.maserb.monitor.entity.schoolConduct.ApplicationRecommendEntity;
import com.maserb.monitor.entity.schoolConduct.SchoolConductItemEntity;
import com.maserb.monitor.entity.schoolConduct.StudentConductRecordEntity;
import com.maserb.monitor.enums.SchoolConductItemTypeEnum;
import com.maserb.monitor.service.author.AuthorService;
import com.maserb.monitor.service.author_manage.UserService;
import com.maserb.monitor.service.schoolConduct.ApplicationRecommendService;
import com.maserb.monitor.service.schoolConduct.SchoolConductItemService;
import com.maserb.monitor.service.schoolConduct.StudentConductRecordService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentParentService;
import com.maserb.monitor.service.schoolStudent.SchoolStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;

/**
 * 应用推优
 */
@RestController
@RequestMapping("/applicationRecommend")
public class ApplicationRecommendController {

    @Autowired
    private ApplicationRecommendService service;

    @RequestMapping(value = {"/", "/index"})
    public ModelAndView getIndex(final HttpServletRequest request) {
        ModelAndView view = new ModelAndView();
        view.setViewName("school_conduct/application_index");
        return view;
    }

    /**
     * 应用推优列表
     * @param name
     * @return
     */
    @RequestMapping("/doListApplication")
    @ResponseBody
    public PageResponseResult<ApplicationRecommendEntity> doListApplication(PageRequestParam page, String name) {
        return AjaxUtils.processPage(page, new Func_T1<Pageable, Page<ApplicationRecommendEntity>>() {
            @Override
            public Page<ApplicationRecommendEntity> invoke(Pageable var1) throws Exception {
                Page<ApplicationRecommendEntity> page = service.findAll(var1,name);
                return page;
            }
        });
    }


    @RequestMapping("/getAddApplication")
    public ModelAndView getAddApplication() {
        ModelAndView result = new ModelAndView();
        result.setViewName("school_conduct/application_add");
        return result;
    }

    @RequestMapping("/doAddApplication")
    @ResponseBody
    public AjaxResult<Boolean> doAddApplication(final HttpServletRequest request, ApplicationRecommendEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                entity.setCreateTime(new Date());
                HttpSession session = request.getSession(true);
                UserEntity userEntity = (UserEntity) session.getAttribute("user");
                entity.setCreateUser(userEntity.getId());
                service.save(entity);
                return true;
            }
        });
    }

    @RequestMapping("/getUpdateApplication/{id}")
    public ModelAndView getUpdateApplication(@PathVariable final String id) {
        ModelAndView result = new ModelAndView();
        ApplicationRecommendEntity entity = service.findOne(id);
        result.setViewName("school_conduct/application_edit");
        result.addObject("entity", entity);
        return result;
    }

    @RequestMapping("/doUpdateApplication")
    @ResponseBody
    public AjaxResult<Boolean> doUpdateApplication(final HttpServletRequest request, ApplicationRecommendEntity entity) {
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                ApplicationRecommendEntity applicationEntity = service.findOne(entity.getId());
                applicationEntity.setName(entity.getName());
                applicationEntity.setDescription(entity.getDescription());
                applicationEntity.setUpdateTime(new Date());
                applicationEntity.setImage(entity.getImage());
                applicationEntity.setAndroidLink(entity.getAndroidLink());
                applicationEntity.setIosLink(entity.getIosLink());
                HttpSession session = request.getSession(true);
                UserEntity userEntity = (UserEntity) session.getAttribute("user");
                entity.setUpdateUser(userEntity.getId());
                service.save(applicationEntity);
                return true;
            }
        });
    }

    /**
     * 删除
     * @param keys
     * @return
     */
    @RequestMapping("/doDelete")
    @ResponseBody
    public AjaxResult<Boolean>doDelete(@RequestParam(value = "keys[]") ArrayList<String> keys){
        return AjaxUtils.process(new Func_T<Boolean>() {
            @Override
            public Boolean invoke() throws Exception {
                service.deleteAllByKeys(keys);
                return true;
            }
        });
    }

}
